﻿using lxwmvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace lxwmvc
{
    public class WeiXin_UserInfo_QYH : IWeiXin
    {
        public static WeiXin_QiYeHao QIYEHAO = null;
        /// <summary>
        /// true 记录用户的位置，并跳转到用户授权的页面
        /// 
        /// 这里是判断企业号，用户是否存在cookies
        /// </summary>
        /// <returns></returns>
        public bool CheckUserInfo(string url, string scope = "snsapi_userinfo")
        {
            //判断用户cookies中的unionid是否存在

            //判断数据库中是否存在

            //是否关注没有太大关系

            //得到cookiesid
            var userid = cookieshelper.getcookie("userid").tostr();
            var openid = cookieshelper.getcookie("openid").tostr();

#if DEBUG
            userid = "8888";
            openid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            cookieshelper.setcookie("userid", userid);
            cookieshelper.setcookie("openid", openid);
#endif

            var pb = HttpContext.Current;

            //如果cookies不存在，就重新拉取这些信息
            if (userid == "" || openid == "")
            {
                pb.Session["login_url"] = pb.Request.Url.PathAndQuery;

                dataloger.info(pb.Request.Url.PathAndQuery);
                QIYEHAO.RedirectWeiXin(url,scope);
                return true;
            }

            return false;
        }

        /// <summary>
        /// true 记录用户的位置，并跳转到用户授权的页面  iq
        /// </summary>
        /// <returns></returns>
        public bool CheckUserInfo_gongzhonghao(string url)
        {
            //判断用户cookies中的unionid是否存在

            //判断数据库中是否存在

            //是否关注没有太大关系

            //得到cookiesid
            var unionid = cookieshelper.getcookie("unionid").tostr();
            var openid = cookieshelper.getcookie("openid").tostr();

            var pb = HttpContext.Current;

            //如果cookies不存在，就重新拉取这些信息
            if (unionid == "" || openid == "")
            {
                pb.Session["login_url"] = pb.Request.Url.PathAndQuery;

                dataloger.info(pb.Request.Url.PathAndQuery);
                weixincontroller.GZH.RedirectWeiXin(url);
                return true;
            }

            return false;
        }

        public  string UrlEncode(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }

            return (sb.ToString());
        }
    }
}
